/**
 * Created by dengtao on 2015/11/13.
 */

(function () {
    window.alert = function (msg, parame) {
        console.log(msg);
        var parame = parame || {};
        //默认配置
        var config = {
            Mask: true,//是否显示遮罩层
            type: 1,
            msgBtnY: '确定',
            msgBtnN: '取消'
        };

        Object.assign(config, parame);

        var promptMask = document.createElement('div');//遮罩层
        promptMask.className = 'eAlertMaskID';
        promptMask.style = 'width: 100%;height: 100%;background: rgba(0, 0, 0, 0.5);z-index: 9999;position: fixed;top: 0px;left: 0px;box-sizing: border-box;';

        var promptBlock = document.createElement('div');//对话框模块
        promptBlock.className = 'promptBlock';
        promptBlock.style = 'width: 70%;background: #FFFFFF;position: absolute;left: 50%;top: 50%;transform: translate(-50%, -50%);border-radius: 10px;padding: 10px 0;box-sizing: border-box;';

        var eAlertstyle = document.createElement('style');//对话框模块
        eAlertstyle.innerHTML = 'html, body {height: 100%;padding: 0;margin: 0;overflow: hidden;}.eAlertMaskID .msgBlock {text-align: center;padding: 10px 20px;line-height: 26px;word-break: break-all;}.eAlertMaskID .ealerttype1 .ybtn {line-height: 40px;text-align: center;}.eAlertMaskID .ealerttype2 {overflow: hidden;padding: 0 25px;}.eAlertMaskID .ealerttype2 > div {line-height: 40px;text-align: center;width: 50%;float: left;}';

        var msgDivAry = ['<div class="msgBlock">', msg, '</div>'];//tips数组
        var alerttype1 = ['<div class="ealerttype1"><div class="ybtn">', config.msgBtnY, '</div></div>'],
            alerttype2 = ['<div class="ealerttype2"><div class="ybtn">', config.msgBtnY, '</div><div class="nbtn">', config.msgBtnN, '</div></div>'];//按钮数组

        var btnAry = [[], alerttype1, alerttype2];//类型数组集
        msgDivAry = [...msgDivAry, ...btnAry[config.type]];//合并内容 选择按钮类型

        var _Body = document.querySelector('body');
        promptMask.appendChild(promptBlock);
        _Body.appendChild(promptMask);//插入遮罩层+内容Block
        _Body.appendChild(eAlertstyle);
        promptBlock.innerHTML = msgDivAry.join('');//插入tip内容和按钮

        //绑定回调
        if (({}).toString.call(config.fn_cb_y) === '[object Function]') {
            document.querySelector('.promptBlock .ybtn').onclick = function () {
                config.fn_cb_y();
                closeEalert();
            };
        }
        if (config.type == 2 && ({}).toString.call(config.fn_cb_n) === '[object Function]') {
            document.querySelector('.promptBlock .nbtn').onclick = function () {
                config.fn_cb_n();
                closeEalert();
            };
        }
        function closeEalert() {
            _Body.style = 'overflow:inherit !important';
            promptMask.style.display = 'none';
        }
    }
})();


var tools = {

    //判断是否是function
    isFunction: function (fn) {
        return ({}).toString.apply(fn) === '[object Function]';
        //以下 效果一样！
        // Object.prototype.toString.call(fn)
        // ({}).toString.apply(fn)
        // toString.call(fn) ;  ,只不过toString会 标记为 未解决的变量或类型说明
        // 等同于 jquery中 $.type();
    },

    //返回顶部
    returnTop: function (step) {
        /*var sTop = document.documentElement.scrollTop == 0 ? document.body.scrollTop : document.documentElement.scrollTop//根据不同浏览器兼容性获取高度
         window.scrollBy(0, step);
         if (sTop > 0) {
         setTimeout(thatObj.returnTop(step), 1);
         }*/
        $('html,body').animate({scrollTop: '0px'}, 800);
    },

    //返回机型
    CheckPhoneSys: function () {
        var phonesys = '';
        var u = navigator.userAgent;
        if (u.indexOf('Android') > -1 || u.indexOf('Linux') > -1) { //安卓手机
            phonesys = "A";
            // window.location.href = "mobile/index.html";
        } else if (u.indexOf('iPhone') > -1) { //苹果手机
            // window.location.href = "mobile/index.html";
            phonesys = "I";
        } else if (u.indexOf('Windows Phone') > -1) { //winphone手机
            phonesys = "W";
            // window.location.href = "mobile/index.html";
        }
        return phonesys;
    },

    //获取url参数
    parseUrlParams: function (url) {
        var reg = /([^?=&]+)=([^?=&]+)/g;
        var paramsObj = {};
        if (url) {
            while (reg.test(url)) {
                paramsObj[RegExp.$1] = RegExp.$2;
            }
        }
        return paramsObj;
    },
    //获取url参数
    parseQueryString: function (url) {
        var reg_url = /^[^\?]+\?([\w\W]+)$/,
            reg_para = /([^&=]+)=([\w\W]*?)(&|$|#)/g,
            arr_url = reg_url.exec(url),
            ret = {};
        if (arr_url && arr_url[1]) {
            var str_para = arr_url[1], result;
            while ((result = reg_para.exec(str_para)) != null) {
                ret[result[1]] = result[2];
            }
        }
        return ret;
    },
    //获取url参数
    split_url: function (parame) {
        var url = window.location.href;
        var param = url.substring(url.indexOf(parame + "=") + (parame.length + 1), url.length);
        var id = param.split("&")[0];
        return id;
    },

    //返回时间戳
    timestamp: function () {
        return new Date().valueOf();
    },
    //ES6 数组去重
    delSame: function (ary) {
        return Array.from(new Set(ary));
    }
}


/**
 ES6中Json、String、Map、Object之间的转换
/!**
 *JsonUitl的实现
 *!/
class JsonUtils extends React.Component {

    /!**
     *字符串转json
     *
     *!/
    static stringToJson(data){
        return JSON.parse(data);
    }
    /!**
     *json转字符串
     *!/
    static jsonToString(data){
        return JSON.stringify(data);
    }
    /!**
     *map转换为json
     *!/
    static mapToJson(map) {
        return JSON.stringify(JsonUtils.strMapToObj(map));
    }
    /!**
     *json转换为map
     *!/
    static jsonToMap(jsonStr){
        return  JsonUtils.objToStrMap(JSON.parse(jsonStr));
    }


    /!**
     *map转化为对象（map所有键都是字符串，可以将其转换为对象）
     *!/
    static strMapToObj(strMap){
        let obj= Object.create(null);
        for (let[k,v] of strMap) {
            obj[k] = v;
        }
        return obj;
    }

    /!**
     *对象转换为Map
     *!/
    static   objToStrMap(obj){
        let strMap = new Map();
        for (let k of Object.keys(obj)) {
            strMap.set(k,obj[k]);
        }
        return strMap;
    }


}
*/


document.onreadystatechange = subSomething; //当页面加载状态改变的时候执行这个方法.
function subSomething() { //页面加载完毕
    if (document.readyState == "complete") {

    }
}
